//
//  AliMNSSampleCode.m
//  AliCloudMNSiOS
//
//  Created by David Chen on 29/03/2017.
//  Copyright © 2017 David Chen. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AliCloudMNSiOS.h"

/*Please config following items*/
NSString* G_Config_Endpoint = @"http://*****.mns.cn-hangzhou.aliyuncs.com";
NSString* G_Config_AccessId = @"*****";
NSString* G_Config_AccessKey = @"*****";
NSString* G_Config_SecurityToken = @"******"; //This is used for STS. Refer to https://help.aliyun.com/document_detail/28657.html for more detail about STS*/

@interface AliMNSSampleCode : NSObject
- (void) createQueue;
- (void) messageProducer;
- (void) messageConsumer;
- (void) deleteQueue;

- (void) createTopic;
- (void) topicMessageProducer;
- (void) deleteTopic;
@end

@implementation AliMNSSampleCode

- (void) createQueue
{
    AliMNSAccount* account = [[AliMNSAccount alloc]
                              initWithEndpoint:G_Config_Endpoint withAccessId:G_Config_AccessId withAccessKey:G_Config_AccessKey withSecurityToken:G_Config_SecurityToken];

    NSString* queueName = @"ali-test-queue-1";
    AliMNSQueue* queue = [account getQueue:queueName];
    AliMNSQueueMeta* queueMeta = [[AliMNSQueueMeta alloc] initWithQueueName:queueName];
    queueMeta.maximumMessageSize = 200;
    queueMeta.visibilityTimeout = 100;
    @try {
        NSString* queueUrl = [queue create:queueMeta];
        NSLog(@"Create queue successfully, queueUrl=%@", queueUrl);
    } @catch(AliMNSClientNetworkException* cne){
        NSLog(@"network error:%@", cne.subMessage);
    } @catch(AliMNSServerException* se){
        NSLog(@"mns server side error: errorCode=%@", se.errorCode);
        // the table for description MNS sever side error is in: https://help.aliyun.com/document_detail/27501.html
    }
}

- (void) messageProducer
{
    // send a message.
    AliMNSAccount* account = [[AliMNSAccount alloc]
                              initWithEndpoint:G_Config_Endpoint withAccessId:G_Config_AccessId withAccessKey:G_Config_AccessKey withSecurityToken:G_Config_SecurityToken];
    NSString* queueName = @"ali-test-queue-1";  // assume this queue is created pepreviously.
    AliMNSQueue* queue = [account getQueue:queueName];

    AliMNSMessage* msg = [AliMNSMessage new];
    msg.messageBody = @"test body.";
    msg.delaySeconds = 1;// delay 1s

    @try {
        AliMNSMessage* retMsg = [queue sendMessage:msg];
        NSLog(@"messageId=%@, messageBodyMd5=%@ receiptHandler=%@", retMsg.messageId, retMsg.messageBodyMd5, retMsg.receiptHandle);// if the message is delay message, it will return receiptHandler as well, and we can use this to delete it before it visible to the consumer.

    } @catch(AliMNSClientNetworkException* cne){
        NSLog(@"network error:%@", cne.subMessage);
    } @catch(AliMNSServerException* se){
        NSLog(@"mns server side error: errorCode=%@", se.errorCode);
        // the table for description MNS sever side error is in: https://help.aliyun.com/document_detail/27501.html
    }
}
- (void) messageConsumer
{
    // receive and delete the message.
    AliMNSAccount* account = [[AliMNSAccount alloc]
                              initWithEndpoint:G_Config_Endpoint withAccessId:G_Config_AccessId withAccessKey:G_Config_AccessKey withSecurityToken:G_Config_SecurityToken];
    NSString* queueName = @"ali-test-queue-1";  // assume this queue is created pepreviously.
    AliMNSQueue* queue = [account getQueue:queueName];
    NSInteger waitSeconds = 10; // this means the request will hold by MNS server side when the queue is empty.during this period, if there is new message, the request will return at once.

    @try {
        AliMNSMessage* getMsg = [queue receiveMessage: waitSeconds];
        //todo: add your logic to deal with the message. Here we only print the body of message.
        NSLog(@"messegeBody=%@", getMsg.messageBody);

        //Important:delete the message after deal with the message successfully.
        [queue deleteMessage:getMsg.receiptHandle];
    } @catch(AliMNSClientNetworkException* cne){
        NSLog(@"network error:%@", cne.subMessage);
    } @catch(AliMNSServerException* se){
        NSLog(@"mns server side error: errorCode=%@", se.errorCode);
        // the table for description MNS sever side error is in: https://help.aliyun.com/document_detail/27501.html
    }
}

- (void) deleteQueue
{
    AliMNSAccount* account = [[AliMNSAccount alloc]
                              initWithEndpoint:G_Config_Endpoint withAccessId:G_Config_AccessId withAccessKey:G_Config_AccessKey withSecurityToken:G_Config_SecurityToken];
    NSString* queueName = @"ali-test-queue-1";
    AliMNSQueue* queue = [account getQueue:queueName];
    @try {
        [queue deleteQueue];
    } @catch(AliMNSClientNetworkException* cne){
        NSLog(@"network error:%@", cne.subMessage);
    } @catch(AliMNSServerException* se){
        NSLog(@"mns server side error: errorCode=%@", se.errorCode);
        // the table for description MNS sever side error is in: https://help.aliyun.com/document_detail/27501.html
    }
}

- (void) createTopic
{
    AliMNSAccount* account = [[AliMNSAccount alloc]
                              initWithEndpoint:G_Config_Endpoint withAccessId:G_Config_AccessId withAccessKey:G_Config_AccessKey withSecurityToken:G_Config_SecurityToken];
    NSString* topicName = @"ali-test-topic-1";
    AliMNSTopic* topic = [account getTopic:topicName];
    AliMNSTopicMeta* topicMeta = [[AliMNSTopicMeta alloc] initWithTopicName:topicName];
    topicMeta.maximumMessageSize = 2000;

    @try {
        NSString* topicUrl = [topic create:topicMeta];
        NSLog(@"topicUrl=%@", topicUrl);
    } @catch(AliMNSClientNetworkException* cne){
        NSLog(@"network error:%@", cne.subMessage);
    } @catch(AliMNSServerException* se){
        NSLog(@"mns server side error: errorCode=%@", se.errorCode);
        // the table for description MNS sever side error is in: https://help.aliyun.com/document_detail/27501.html
    }
}

- (void) topicMessageProducer
{
    /* publish a message */
    AliMNSAccount* account = [[AliMNSAccount alloc]
                              initWithEndpoint:G_Config_Endpoint withAccessId:G_Config_AccessId withAccessKey:G_Config_AccessKey withSecurityToken:G_Config_SecurityToken];
    NSString* topicName = @"ali-test-topic-1";
    AliMNSTopic* topic = [account getTopic:topicName];
    AliMNSTopicMessage* msg = [AliMNSTopicMessage new];
    msg.messageBody = @"my publish message";

    @try {
        AliMNSTopicMessage* retMsg = [topic publishMessage:msg];
        NSLog(@"messageId=%@, messageBodyMd5=%@", retMsg.messageId, retMsg.messageBodyMd5);
    } @catch(AliMNSClientNetworkException* cne){
        NSLog(@"network error:%@", cne.subMessage);
    } @catch(AliMNSServerException* se){
        NSLog(@"mns server side error: errorCode=%@", se.errorCode);
        // the table for description MNS sever side error is in: https://help.aliyun.com/document_detail/27501.html
    }
}

- (void) deleteTopic
{
    AliMNSAccount* account = [[AliMNSAccount alloc]
                              initWithEndpoint:G_Config_Endpoint withAccessId:G_Config_AccessId withAccessKey:G_Config_AccessKey withSecurityToken:G_Config_SecurityToken];
    NSString* topicName = @"ali-test-topic-1";
    AliMNSTopic* topic = [account getTopic:topicName];

    @try {
        [topic deleteTopic];
    } @catch(AliMNSClientNetworkException* cne){
        NSLog(@"network error:%@", cne.subMessage);
    } @catch(AliMNSServerException* se){
        NSLog(@"mns server side error: errorCode=%@", se.errorCode);
        // the table for description MNS sever side error is in: https://help.aliyun.com/document_detail/27501.html
    }
}
@end
